<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="计划名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="计划名称"></el-input>
      </el-form-item>
      <el-form-item label="人脸分组" prop="faceGroupIndexCodes">
        <el-select v-model="dataForm.faceGroupIndexCodes" clearable placeholder="人脸分组">
          <el-option
            v-for="item in faceGroupList"
            :key="item.indexCode"
            :label="item.name"
            :value="item.indexCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="监控点" prop="cameraIndexCodes">
        <el-select v-model="dataForm.cameraIndexCodes" clearable placeholder="请选择">
          <el-option
            v-for="item in monitorList"
            :key="item.cameraIndexCode"
            :label="item.cameraName"
            :value="item.cameraIndexCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="识别资源类型" prop="recognitionResourceType">
        <el-select v-model="dataForm.recognitionResourceType" @change="changeSelect" clearable placeholder="请选择">
          <el-option
            v-for="item in faceResourceTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="识别资源名称" prop="recognitionResourceIndexCodes">
        <el-select v-model="dataForm.recognitionResourceIndexCodes" clearable placeholder="请选择">
          <el-option
            v-for="item in faceResourceLists"
            :key="item.indexCode"
            :label="item.name"
            :value="item.indexCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="执行星期" prop="dayOfWeek">
        <el-select v-model="dataForm.dayOfWeek" clearable placeholder="请选择">
          <el-option
            v-for="item in dayOfWeeks"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="抓拍开始时间" prop ="time1">
        <el-time-select
          placeholder="起始时间"
          v-model="dataForm.time1"
          :picker-options="{
      start: '00:01',
      step: '00:01',
      end: '23:59',
    }">
        </el-time-select>
      </el-form-item>
      <el-form-item label="抓拍结束时间" prop ="time2">
        <el-time-select
          placeholder="结束时间"
          v-model="dataForm.time2"
          :picker-options="{
      start: '00:01',
      step: '00:01',
      end: '23:59',
      minTime: dataForm.time1
    }">
        </el-time-select>
      </el-form-item>
      <el-form-item label="计划描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="计划描述"></el-input>
      </el-form-item>
      <el-form-item label="识别计划的阈值" prop="threshold">
        <el-input v-model="dataForm.threshold" placeholder="识别计划的阈值"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {getObj, addObj, putObj, fetchList,getMonitorList,getFaceResourceList,getFaceGroupList} from '@/api/hik/strange'

  export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        faceGroupList:[],
        monitorList:[],
        faceResourceList:[],
        faceResourceLists:[],
        faceResourceTypeList: [{
          value: 'SUPER_BRAIN',
          label: '超脑'
        }, {
          value: 'FACE_RECOGNITION_SERVER',
          label: '脸谱'
        }, {
          value: 'COMPARISON',
          label: '深眸'
        }],
        dayOfWeeks:[
          {
            value: '1',
            label: '星期一'
          }, {
            value: '2',
            label: '星期二'
          }, {
            value: '3',
            label: '星期三'
          }, {
            value: '4',
            label: '星期四'
          }, {
            value: '5',
            label: '星期五'
          }, {
            value: '6',
            label: '星期六'
          }, {
            value: '7',
            label: '星期天'
          }
        ],
        dataForm: {
          id: '',
          name: '',

          faceGroupIndexCodes: '',

          snapStartTime: '',

          snapEndTime: '',

          cameraIndexCodes: '',

          flag: '2',

          recognitionResourceType: '',

          recognitionResourceIndexCodes: '',

          dayOfWeek: '',
          description: '',
          threshold: '',
          time2:'',
          time1:'',
        },
        dataRule: {
          name: [
            { required: true, message: '计划名称不能为空', trigger: 'blur' }
          ],
          time1: [
            { required: true, message: '抓拍开始时间不能为空', trigger: 'blur' }
          ],
          time2: [
            { required: true, message: '抓拍结束时间不能为空', trigger: 'blur' }
          ],
          faceGroupIndexCodes: [
            { required: true, message: '人脸分组不能为空', trigger: 'blur' }
          ],
          cameraIndexCodes: [
            { required: true, message: '监控点不能为空', trigger: 'blur' }
          ],
          recognitionResourceType: [
            { required: true, message: '识别资源类型不能为空', trigger: 'blur' }
          ],
          recognitionResourceIndexCodes: [
            { required: true, message: '识别资源名称不能为空', trigger: 'blur' }
          ],
          dayOfWeek: [
            { required: true, message: '星期不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '计划描述不能为空', trigger: 'blur' }
          ],
          threshold: [
            { required: true, message: '识别计划的阈值不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.getFaceGroupList();
        this.getMonitorList();
        this.getFaceResourceList();
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
              console.log(response.data.data)
              //时间回显处理 将data 返回的时间push进去
              // let tmpArr = []
              let time1 =this.dataForm.snapStartTime.substring(11,this.dataForm.snapStartTime.length-3);
              let time2 =this.dataForm.snapEndTime.substring(11,this.dataForm.snapEndTime.length-3);
              this.$set(this.dataForm,'time1',time1)
              this.$set(this.dataForm,'time2',time2)
              // this.dataForm.dateList = tmpArr
            })
          }
        })
      },
      // 获取数据列表
      getFaceGroupList () {
        this.dataListLoading = true
        getFaceGroupList(Object.assign({})).then(response => {
          this.faceGroupList=response.data.data;
          // console.log(response.data.data);
        })
        this.dataListLoading = false
      },
      // 获取数据列表
      getMonitorList () {
        this.dataListLoading = true
        getMonitorList(Object.assign({})).then(response => {
          this.monitorList=response.data.data;
          // console.log(response.data.data);
        })
        this.dataListLoading = false
      },
      // 获取数据列表
      getFaceResourceList () {
        this.dataListLoading = true
        getFaceResourceList(Object.assign({})).then(response => {
          this.faceResourceList=response.data.data;
          // console.log(response.data.data);
        })
        this.dataListLoading = false
      },
      changeSelect() {
        // 联动子级滞空
        this.faceResourceLists = [];
        this.dataForm.recognitionResourceIndexCodes="";
        // 循环遍历父级
        for (const k in this.faceResourceList) {
          console.log()
          if (this.dataForm.recognitionResourceType === this.faceResourceList[k].recognitionResourceType) {
            // 核心代码在这里 进行赋值操作
            this.faceResourceLists.push(this.faceResourceList[k]);
          }
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {

              this.dataForm.snapStartTime = '2023-5-15 '+this.dataForm.time1+':00';
              this.dataForm.snapEndTime = '2023-5-15 '+this.dataForm.time2+':00';
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              this.dataForm.snapStartTime = '2023-5-15 '+this.dataForm.time1+':00';
              this.dataForm.snapEndTime = '2023-5-15 '+this.dataForm.time2+':00';
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      }
    }
  }
</script>
